package com.xialinrui.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xialinrui.dto.DailyAcquirementDto;
import com.xialinrui.dto.MonthlyAcquirementDto;
import com.xialinrui.dto.YearlyAcquirementDto;
import com.xialinrui.entity.Acquirement;
import com.xialinrui.entity.ResponseResult;
import com.xialinrui.service.AcquirementService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xialinrui
 */
@RestController
@Slf4j
@RequestMapping("/acquirement")
public class AcquirementController {

    @Resource
    private AcquirementService acquirementService;


    /**
     * 添加一条记录
     *
     * @param date
     * @return
     */
    @PostMapping("/add")
    public ResponseResult add(@RequestParam Date date) {
        QueryWrapper<Acquirement> acquirementQueryWrapper = new QueryWrapper<>();
        acquirementQueryWrapper.eq("date", date);
        Acquirement ifHas = acquirementService.getOne(acquirementQueryWrapper);
        if (ifHas != null) {
            return new ResponseResult(200, "添加成功");
        }
        acquirementService.add(new Acquirement(null, date, 0.0, 0, 0));
        return new ResponseResult(200, "添加成功");
    }

    /**
     * 根据起始日期和末尾日期查询金钱获得、爱心获得、学习时长
     *
     * @param from
     * @param to
     * @return
     */
    @GetMapping("/things/daily")
    public ResponseResult<Map<String, List<Object>>> thingsDaily(@RequestParam String from, @RequestParam String to) {
        List<DailyAcquirementDto> resList = acquirementService.thingsDaily(from, to);
        Map<String, List<Object>> resMap = new HashMap<>(4);
        resMap.put("日期", new ArrayList<>());
        resMap.put("爱心", new ArrayList<>());
        resMap.put("金钱", new ArrayList<>());
        resMap.put("学习时长", new ArrayList<>());
        for (DailyAcquirementDto dailyAcquirementDto : resList) {
            resMap.get("日期").add(dailyAcquirementDto.getDay());
            resMap.get("爱心").add(dailyAcquirementDto.getLoveNumbers());
            resMap.get("金钱").add(dailyAcquirementDto.getMoneyNumbers());
            resMap.get("学习时长").add(dailyAcquirementDto.getStudyTime());
        }
        return new ResponseResult(200, "查询成功", resMap);
    }

    /**
     * 根据起始月份和末尾月份查询每月金钱获得、爱心获得、学习时长
     *
     * @param from
     * @param to
     * @return
     */
    @GetMapping("/things/monthly")
    public ResponseResult<Map<String, List<Object>>> thingsMonthly(@RequestParam String from, @RequestParam String to) {
        List<MonthlyAcquirementDto> resList = acquirementService.thingsMonthly(from, to);
        Map<String, List<Object>> resMap = new HashMap<>(4);
        resMap.put("月份", new ArrayList<>());
        resMap.put("爱心", new ArrayList<>());
        resMap.put("金钱", new ArrayList<>());
        resMap.put("学习时长", new ArrayList<>());
        for (MonthlyAcquirementDto monthlyAcquirementDto : resList) {
            resMap.get("月份").add(monthlyAcquirementDto.getMonth());
            resMap.get("爱心").add(monthlyAcquirementDto.getLoveNumbers());
            resMap.get("金钱").add(monthlyAcquirementDto.getMoneyNumbers());
            resMap.get("学习时长").add(monthlyAcquirementDto.getStudyTime());
        }
        return new ResponseResult(200, "查询成功", resMap);
    }

    /**
     * 根据起始年份和末尾年份查询每年金钱获得、爱心获得、学习时长
     *
     * @param from
     * @param to
     * @return
     */
    @GetMapping("/things/yearly")
    public ResponseResult<Map<String, List<Object>>> thingsYearly(@RequestParam String from, @RequestParam String to) {
        List<YearlyAcquirementDto> resList = acquirementService.thingsYearly(from, to);
        Map<String, List<Object>> resMap = new HashMap<>(4);
        resMap.put("年份", new ArrayList<>());
        resMap.put("爱心", new ArrayList<>());
        resMap.put("金钱", new ArrayList<>());
        resMap.put("学习时长", new ArrayList<>());
        for (YearlyAcquirementDto yearlyAcquirementDto : resList) {
            resMap.get("年份").add(yearlyAcquirementDto.getYear());
            resMap.get("爱心").add(yearlyAcquirementDto.getLoveNumbers());
            resMap.get("金钱").add(yearlyAcquirementDto.getMoneyNumbers());
            resMap.get("学习时长").add(yearlyAcquirementDto.getStudyTime());
        }
        return new ResponseResult(200, "查询成功", resMap);
    }

}
